//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Files.Components;
using CommunityServer.Controls;

namespace CommunityServer.Files.Controls
{
	/// <summary>
	/// Summary description for FolderGroupListing.
	/// </summary>
	public class FolderGroupsListing : FileGalleryTemplatedWebControl
	{

		#region Child Controls

		private Repeater folderGroups;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			folderGroups = (Repeater)FindControl("FolderGroups");
			folderGroups.ItemDataBound += new RepeaterItemEventHandler(folderGroups_ItemDataBound);
		}

		#endregion

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			DataBind();
		}

		public override void DataBind()
		{
			base.DataBind();

			folderGroups.DataSource = Folders.GetFolderGroups(CSContext.Current.UserID, true, false, false);
			folderGroups.DataBind();
		}

		private void folderGroups_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Group group = e.Item.DataItem as Group;
			if (group == null)
				return;

			switch(e.Item.ItemType)
			{
				case ListItemType.AlternatingItem:
				case ListItemType.Item:

					HyperLink name = e.Item.FindControl("GroupName") as HyperLink;
					if (name != null)
					{
						name.Text = group.Name;
						name.NavigateUrl = FileGalleryUrls.Instance().ViewFolderGroup(group.GroupID);
					}

					Repeater folders = (Repeater) e.Item.FindControl("Folders");
					if (folders != null)
					{
						folders.ItemDataBound += new RepeaterItemEventHandler(folders_ItemDataBound);
						folders.DataSource = Folders.GetFoldersByGroupID(group.GroupID, false, true, false);
						folders.DataBind();
					}

					break;
			}
		}

		private void folders_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			Folder folder = e.Item.DataItem as Folder;

			switch(e.Item.ItemType)
			{
				case ListItemType.AlternatingItem:
				case ListItemType.Item:

					HyperLink name = e.Item.FindControl( "Name" ) as HyperLink;
					IText files = TextControlHelper.Create(e.Item.FindControl("Files"));
					IText comments = TextControlHelper.Create(e.Item.FindControl("Comments"));
					IText description = TextControlHelper.Create(e.Item.FindControl("Description"));

					if(name != null)
					{
						name.Text = folder.Name;
						name.NavigateUrl = FileGalleryUrls.Instance().ViewFolder(folder.ApplicationKey);
					}

					if (files != null)
						files.Text = folder.TotalThreads.ToString();

					if (comments != null)
						comments.Text = (folder.TotalPosts - folder.TotalThreads).ToString();

					if (description != null)
						description.Text = folder.Description;

					break;
			}
		}
	}
}
